﻿function Application() {

    Application.superclass.constructor.call(this);
    var instance = this;
    
    var control;
    var type; 
    var name;
    var error;
    var interval = 15;
    var str = "";
    var arrT = [];
    var arrB = [];
    var bullet = 0;
    var bulletNamber = 0;
    /* canvas */
    var titl_canvas;
    var titl_context;
    var maincanvas;
    var context;
        /* lable */
    var canB = document.createElement('canvas');
    var ctxB = canB.getContext('2d');
    var canT = document.createElement('canvas');
    var ctxT = canT.getContext('2d');
    var canBu = document.createElement('canvas');
    var ctxBu = canBu.getContext('2d');
    
    var str={};
    
    // Create a new image.

       var imgKr = new Image();
       var imgBr = new Image();
       imgKr.src = 'img/korpusright.gif';
       imgBr.src = 'img/turretright.gif';   

    /*Constructor*/
    (function () {
        document.getElementById('drow1').remove();
        document.getElementById('drow2').remove();
        error = 0;
        assign_default();
    })();

    /*Public section*/

    /*Private section*/
    function sendData(obj) {  /*функция отправки str*/
            var settings = new Object();
            settings.url = "http://127.0.0.1:1337";
            settings.type = "POST";
            settings.data = str;
            settings.async = false;
            str =   jQuery.ajax(settings).responseText;
} 
    function Game(){

          var iteration = setInterval(function () {
             str={};
             context.clearRect(0, 0, canvasOdj.width, canvasOdj.height);
             context.strokeRect(0,30,600,1);
             context.font = "italic 30pt Arial";
             context.fillStyle = "#000";
             context.fillText(name,20,30);
             context.fillStyle = "#F00";
             context.fillText(tank.HP+"/"+tank.maxHP,400,30);
             tank.Replace();
             str.a=tank;
             if (bullet!=0){
                bullet.Replace();
                if (tank.HP>0) {str.b=bullet;}
                bullet=0;
            }
            str=JSON.stringify(str);
            sendData(str);
            str=JSON.parse(str);
            arrT=str.a;
            arrB=str.b;
              if (arrB!="" && arrB!=null && arrB!=undefined){
                 for (var keyB=bulletNamber;bulletNamber<arrB.length; bulletNamber++) {
                  for (var i=50; i<150; i=i+10){   
                     var shag=i;
                     var degree1=(arrB[keyB].degrees/360)*Math.PI*2; 
                     var degree2=(90-arrB[keyB].degrees)/360*Math.PI*2;
                     var katetX=shag*(Math.sin(degree2));
                     var katetY=shag*(Math.sin(degree1));
                     arrB[keyB].x=arrB[keyB].x+katetX;
                     arrB[keyB].y=arrB[keyB].y+katetY;
                         DrawB(arrB[keyB]);
                         //console.log(JSON.stringify(arrB)+'\n');
                         for (var key in arrT)
                      {   if (i>70 && (arrB[keyB].x>arrT[key].x-40) && (arrB[keyB].x<arrT[key].x+40) && (arrB[keyB].y>arrT[key].y-40) && (arrB[keyB].y<arrT[key].y+40))
                          {
                              arrT[key].HP=arrT[key].HP-arrB[key].damage
                               if (arrT[key].nick==tank.nick && tank.HP>0){tank.HP=tank.HP-arrB[key].damage;}
                              };
                            //  console.log(arrT[0].HP);
                    
                      }
                  }  
                 }
             }
              if (arrT!='' && arrT!=null ) {
                  for (var key in arrT) {
              // console.log(arrT[key].HP+'\n');
       if (arrT[key].HP>0) {
	       tank4.nick=arrT[key].nick;
           tank4.corpusWidth  = arrT[key].corpusWidth;
           tank4.corpusHeight = arrT[key].corpusHeight;
           tank4.turretWidth = arrT[key].turretWidth;
           tank4.turretHeight = arrT[key].turretHeight;
		   tank4.x=arrT[key].x;
		   tank4.y=arrT[key].y;
		   tank4.degreesT=arrT[key].degreesT;
		   tank4.degreesB=arrT[key].degreesB;
		 //  alert('Ayyy');
	   //    DrawT(arrT[key]); arrT[key]=''; 
		   tank4.Draw();
	   }
                   }
       }
             },interval);
    }
 /*   function DrawT(tanks){
         // drow corpus 
                var sizeT = tanks.corpusWidth;
        
                canT.width = sizeT*2  ;
                canT.height = sizeT*2  ;
                ctxT.translate(sizeT, sizeT);
                ctxT.rotate(tanks.degreesT.degree());
                ctxT.drawImage(imgKr, -tanks.corpusWidth/2, -tanks.corpusHeight/2,tanks.corpusWidth,tanks.corpusHeight);
                context.drawImage(canT, tanks.x-sizeT, tanks.y-sizeT);
                context.font = "italic 10pt Arial";
                context.fillStyle = "#000";
                context.fillText(tanks.nick,tanks.x-tanks.nick.length,tanks.y-sizeT/2*1.5);
             
          // drow turret 
       
                var sizeB = tanks.corpusWidth;
        
                canB.width = sizeB*2  ;
                canB.height = sizeB*2  ;
                ctxB.translate(sizeB, sizeB);
                ctxB.rotate(tanks.degreesB.degree());
                ctxB.drawImage(imgBr, -tanks.turretWidth/2+10, -tanks.turretHeight/2,tanks.turretWidth,tanks.turretHeight);
                context.drawImage(canB, tanks.x-sizeB, tanks.y-sizeB);
     }
     */
     function DrawB(bullets){
                var sizeBu= 6;
        
                canBu.width = sizeBu*2  ;
                canBu.height = sizeBu*2  ;
                ctxBu.translate(sizeBu, sizeBu);
                ctxBu.rotate(bullets.degrees.degree());
                ctxBu.fillRect(-3, -2,6,4);
                context.drawImage(canBu, bullets.x-sizeBu, bullets.y-sizeBu);
     }   
    
    /*Assign listeners functions*/

    function assign_default() {
        /*button Mouse*/
        document.getElementById("Mouse").onclick = function () {
            document.getElementById('Mouse').style.borderColor =  'red' ;
            document.getElementById('Keyboard').style.borderColor =  'blue' ;
            document.getElementById('helpBox').innerHTML = "Help: <br><br><br>Mouse: <br><br>"+
                    "Mouse right button -> move<br>"+
                    "Mause left button -> fire<br>"+
                    "Use left and right arrow -> <br>"+
                    "rotate tanks turret left/right";
            control = 1;

        };
        /*button Keyboard*/ 
        document.getElementById('Keyboard').onclick = function () {
            document.getElementById('Keyboard').style.borderColor =  'red' ;
            document.getElementById('Mouse').style.borderColor =  'blue' ;
            document.getElementById('helpBox').innerHTML = "Help: <br><br><br> Keyboard: <br><br>"+
                    "Press space -> fire<br>"+
                    "key W -> move forward<br>"+
                    "key S -> move back<br>"+
                    "key A -> rotate tank left<br>"+
                    "key D -> rotate tank right<br>"+
                    "Press left and right arrow -> <br>"+
                    "rotate tanks turret left/right";
            control = 2;
        };
        /*button Tank1*/ 
        document.getElementById('Tank1').onclick = function () {
            document.getElementById('Tank1').style.borderColor =  'red' ;
            document.getElementById('Tank2').style.borderColor =  'blue' ;
            document.getElementById('Tank3').style.borderColor =  'blue' ;
            document.getElementById('helpBox').innerHTML = "Help: <br><br><br> Tank1: <br><br>"+
                    "Speed = 1<br>"+
                    "Size = 60<br>"+
                    "Hp = 300<br>"+
                    "Fire rate = 1bps<br>"+
                    "Damage = 75<br>";
            type = 1;
        };
        /*button Tank2*/ 
        document.getElementById('Tank2').onclick = function () {
            document.getElementById('Tank1').style.borderColor =  'blue' ;
            document.getElementById('Tank2').style.borderColor =  'red' ;
            document.getElementById('Tank3').style.borderColor =  'blue' ;
            document.getElementById('helpBox').innerHTML = "Help: <br><br><br> Tank1: <br><br>"+
                    "Speed = 3<br>"+
                    "Size = 40<br>"+
                    "Hp = 150<br>"+
                    "Fire rate = 3bps<br>"+
                    "Damage = 50<br>";
            type=2;
        };
        /*button Tank3*/ 
        document.getElementById('Tank3').onclick = function () {
            document.getElementById('Tank1').style.borderColor =  'blue' ;
            document.getElementById('Tank2').style.borderColor =  'blue' ;
            document.getElementById('Tank3').style.borderColor =  'red' ;
            document.getElementById('helpBox').innerHTML = "Help: <br><br><br> Tank1: <br><br>"+
                    "Speed = 0.5<br>"+
                    "Size = 90<br>"+
                    "Hp = 500<br>"+
                    "Fire rate = 0.5bps<br>"+
                    "Damage = 100<br>";
            type=3;
        };
        /*Drow tanks examples*/
        titl_canvas = document.getElementById("title_can");
        titl_canvas.width = 400  ;
        titl_canvas.height = 150 ;
        if (titl_canvas && titl_canvas.getContext) {
            titl_context = titl_canvas.getContext('2d');
        }
        tank1 = new Tank(titl_context,imgKr,imgBr,66,75,60,60,1,1,1.5,300,75,1,1,'1');
        tank2 = new Tank(titl_context,imgKr,imgBr,200,75,40,40,3,3,3,150,50,3,1,'2');
        tank3 = new Tank(titl_context,imgKr,imgBr,333,75,90,90,0.5,0.5,1,500,100,0.5,1,'3');
        tank1.Draw();
        tank2.Draw();
        tank3.Draw();
        titl_context.strokeRect(133,0,134,150);
        
        /* button start */
        document.getElementById('start').onclick = function()
        {
           /* data checking */
            name = document.getElementById('in_nickname').value;
            if (name === '')
               {
                   document.getElementById('helpBox').innerHTML = "Help: <br><br><br> Error!!! <br><br>"+
                       "Nick field is not filled!!";
                  error=1;
               };
            if (control === undefined)
               {
                   document.getElementById('helpBox').innerHTML = "Help: <br><br><br> Error!!! <br><br>"+
                       "You din't select a control!!";
                  error=1;
               };   
           if (type === undefined)
               {
                   document.getElementById('helpBox').innerHTML = "Help: <br><br><br> Error!!! <br><br>"+
                       "You din't select a tank!!";
                  error=1;
               };   
           if (error!==1)
               {
                   document.getElementById('helpBox').remove();
                   b = document.getElementById('main');
                   b.style.height = '600px';
                   b.style.width = '600px';
                   b.style.left = 0;
                   b.style.marginLeft = '350px';
                   b.innerHTML = '<canvas id = "main_can" onClick = "tank.move(event);"> oncontextMenu="tank.move(event);return false"</canvas>';
                   canvasOdj = document.getElementById("main_can");
                   canvasOdj.height = 600;
                   canvasOdj.width = 600;
                   context = canvasOdj.getContext('2d');
                   tank4 = new Tank(context,imgKr,imgBr,66,75,60,60,1,1,1.5,300,75,1,1,'1');
                   if (type===1){
                         tank = new Tank(context,imgKr,imgBr,50+Math.random()*500,50+Math.random()*500,60,60,1,1,1.5,300,75,1,control,name);
                   } else {
                         if (type===2){
                              tank =  new Tank(context,imgKr,imgBr,50+Math.random()*500,50+Math.random()*500,40,40,3,3,3,150,50,3,control,name);
                         } else {
                              tank = new Tank(context,imgKr,imgBr,50+Math.random()*500,50+Math.random()*500,90,90,0.5,0.5,1,500,100,0.5,control,name);
                         }
                   }
                   Game();                  
               }     
        };
    };
    
 function sendData(obj) {  /*функция отправки str*/
            var settings = new Object();
            settings.url = "http://127.0.0.1:1337";
            settings.type = "POST";
            settings.data = str;
            settings.async = false;
            str =   jQuery.ajax(settings).responseText;
}
 instance.createBullet = function() { 
    bullet = new Bullet(tank.x,tank.y,tank.degreesB,tank.damage);
}
    
    

}


window.extendClass(Application, Object);



